<?php

include_once('user_controller.php');

class Electoral_Roll extends User_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('electoral_roll_model');
		$this->load->library('session');
	}

    public function start()
    {
        $this->load->helper('url');

        $zone_type = $this->session->userdata('zone_type');
        
        $zones = $this->electoral_roll_model->get_electoral_zones($zone_type);
     
        $start_zone = $zones[0]->id;   
        
        redirect('electoral_roll/view/' . $start_zone);
    }
    
	public function view($zone)
	{
		$this->load->helper('url');

	    $this->load->library('pagination');
	    $config['base_url'] = base_url().'index.php/electoral_roll/view/' . $zone;
	    $config['uri_segment'] = 4;
	    $config['per_page'] = 50;
	    $config['full_tag_open'] = '<p>';
	    $config['full_tag_close'] = '</p>';

		$data['roll'] = $zone;

		$zone_props = $this->electoral_roll_model->get_zone($zone);
		
        $data['title'] = 'Electoral Roll: ' . $zone_props->display_name;

		$name_search = $this->session->userdata('name_search');
		$address_search = $this->session->userdata('address_search');

		if (!$name_search)
		{
			$name_search = "";
		}
		if (!$address_search)
		{
			$address_search = "";
		}

		$data['name_search'] = $name_search;
		$data['address_search'] = $address_search;

//		echo "name_search: [" . $name_search . "]. address_search: [" . $address_search . "]<br/>";

		if (!$name_search && !$address_search)
		{
			// all entries
	    	$config['total_rows'] = $this->electoral_roll_model->count_entries($zone);
			$data['entries'] = $this->electoral_roll_model->get_entries($zone, $config['per_page'], (int) $this->uri->segment(4));
		}
		else
		{
	    	$config['total_rows'] = $this->electoral_roll_model->count_quick_search($zone, $name_search, $address_search);
			$data['entries'] = $this->electoral_roll_model->get_quick_search($zone, $name_search, $address_search, $config['per_page'], (int) $this->uri->segment(4));
		}

        $zone_type = $this->session->userdata('zone_type');
		$data['rolls'] = $this->electoral_roll_model->get_electoral_zones($zone_type);

		$this->pagination->initialize($config);

		$data['content'] = 'electoral_roll_table';
		$this->load->view('layout/main', $data);

	}


	public function submit_search()
	{
		$this->load->helper('url');

		$this->session->unset_userdata('name_search');
		$this->session->unset_userdata('address_search');

		$roll = $this->input->post('roll');

        $name_search = trim ($this->input->post('name'));
        $address_search = trim ($this->input->post('address'));

		$terms = array();

		if (strlen ($name_search) > 0)
		{
			$terms ['name_search'] = $name_search;
		}
		if (strlen ($address_search) > 0)
		{
			$terms ['address_search'] = $address_search;
		}

		if (count ($terms) > 0)
		{
            $this->session->set_userdata($terms);
        }

        redirect('electoral_roll/view/' . $roll);
	}

	public function clear_search()
	{
		$this->load->helper('url');

		$this->session->unset_userdata('name_search');
		$this->session->unset_userdata('address_search');

		$roll = $this->input->post('roll');

        redirect('electoral_roll/view/' . $roll);
	}
}
?>